﻿var tableLeft;
var urlLeft;
$(function () {
    urlLeft = "/Role/GetUserNotInRole?RoleId=" + window.location.href.split('=')[1] + "&search="
    tableLeft = $().hiTable({
        tableId: "tbLeft",
        pageBarId: "PageBarLeft",
        ajaxURL: urlLeft,
        DefaultSortTitle: "Id",
        DefaultSortOrder: "1",
        columnsConfig: [
            { Title: 'Id', IsCheckbox: true },
            { Title: 'EmployeeNo' },
            { Title: 'DisplayName' }
        ]
    });

    //PageLoad时，加载数据
    tableLeft.loadData(parseInt($("#perPageLeft").val()), 0);
    //改变每页显示条数
    $("#perPageLeft").change(function () {
        tableLeft.loadData(parseInt($(this).val()), 0);
    });
});
var tableRight;
var urlRight;
$(function () {
    urlRight = "/Role/GetUserRole?RoleId=" + window.location.href.split('=')[1]
    tableRight = $().hiTable({
        tableId: "tbRight",
        pageBarId: "PageBarRight",
        ajaxURL: urlRight,
        DefaultSortTitle: "Id",
        DefaultSortOrder: "1",
        columnsConfig: [
            { Title: 'Id', IsCheckbox: true },
            { Title: 'EmployeeNo' },
            { Title: 'DisplayName' }
        ]
    });
    //PageLoad时，加载数据
    tableRight.loadData(parseInt($("#perPageRight").val()), 0);
    //改变每页显示条数
    $("#perPageRight").change(function () {
        tableRight.loadData(parseInt($(this).val()), 0);
    });
})

$(function () {
    //搜索未选
    $("#imgSearchLeft").click(function () {
        $(function () {
            var Search = $.trim($("#TextUserLeft").val());
            urlLeft = "/Role/GetUserNotInRole?RoleId=" + window.location.href.split('=')[1] + "&search=" + Search;
            tableLeft = $().hiTable({
                tableId: "tbLeft",
                pageBarId: "PageBarLeft",
                ajaxURL: urlLeft,
                DefaultSortTitle: "Id",
                DefaultSortOrder: "1",
                columnsConfig: [
                    { Title: 'Id', IsCheckbox: true },
                    { Title: 'EmployeeNo' },
                    { Title: 'DisplayName' }
                ]
            });
            //PageLoad时，加载数据
            tableLeft.loadData(parseInt($("#perPageLeft").val()), 0);
            //改变每页显示条数
            $("#perPageLeft").change(function () {
                tableLeft.loadData(parseInt($(this).val()), 0);
            });
        });
    })
    //增加具有此权限的人员
    $("#Add").click(function () {
        var ids = tableLeft.getSelected();
        var v = "";

        for (var i = 0; i < ids.length; i++)
            v += ids[i] + ";";
        //alert(v);
        $.post("/Role/AddUserToRole", {
            roleId: window.location.href.split('=')[1],
            id: v
        }, function (Data) {
            tableLeft.loadData(parseInt($("#perPageLeft").val()), 0);
            tableRight.loadData(parseInt($("#perPageRight").val()), 0);
        });
    })
    //删除具有此权限的人员
    $("#Delete").click(function () {
        var ids = tableRight.getSelected();
        var v = "";
    
        for (var i = 0; i < ids.length; i++)
            v += ids[i] + ";";
        //alert(v);
        $.post("/Role/DeleteUserFromRole", {
            roleId: window.location.href.split('=')[1],
            id: v
        }, function (Data) {
            tableLeft.loadData(parseInt($("#perPageLeft").val()), 0);
            tableRight.loadData(parseInt($("#perPageRight").val()), 0);
        });
    })
})